package ch.myDungeons.view;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;

import ch.myDungeons.io.LanguagePack;
import ch.myDungeons.game.MyDungeon;

/**
 * The console in the game containing the game notifications
 * @author Grii
 *
 */

public class Console extends JPanel{
	private static final long serialVersionUID = -2932107600156546839L;

	/** The text area containing the console output */
	private JTextArea consoleOutput = new JTextArea ("");
	
	/** The text area containing the command line input */
	public JTextField consoleInput = new JTextField ("");
	
	/** A scrollpane to make the console output scrollable */
	private JScrollPane scroll;

	
	private String lastCommand = "";
	
	private boolean normalStringRequested = false;
	
	/** Constructor of the console */
	public Console(int sizeX, int sizeY){
		this.setLayout(null);
		this.setSize(sizeX, sizeY);
		this.setBackground(Color.white);

		consoleOutput.setSize(sizeX, sizeY - 30);
		consoleOutput.setEditable(false);
		//consoleOutput.setFont(new Font(Font.MONOSPACED, 0, 12));
		consoleOutput.setLineWrap(true);
		consoleOutput.setWrapStyleWord(true);
		
		scroll = new JScrollPane (consoleOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setSize(sizeX - 11, sizeY - 30);
		scroll.setLocation(10, 0);
		scroll.setBorder(null);
		
		consoleInput.setLocation(0, sizeY - 30);
		consoleInput.setSize(sizeX, 30);
		consoleInput.addKeyListener(inputListener);
		consoleInput.requestFocus();
		//consoleInput.setFont(new Font(Font.MONOSPACED, 0, 12));
		
		scroll.setAutoscrolls(true);
		add(scroll);
		add(consoleInput);
	}
	
	/** Print out something into the console */
	public void println(String str){
		str = LanguagePack.capitalizeString(str);
		consoleOutput.append("\n" + str);
		
		consoleOutput.setCaretPosition(consoleOutput.getDocument().getLength());
		
		if (consoleOutput.getLineCount() > 1000) {
			int end = consoleOutput.getLineCount() - 1000;
			try {
				end = consoleOutput.getLineEndOffset(end);
			} catch (BadLocationException e) {}
			consoleOutput.replaceRange("", 0, end);
		}
	}
	
	/** Clear everything in the console */
	public void clearOutput(){
		consoleOutput.setText("");
		consoleOutput.setCaretPosition(consoleOutput.getDocument().getLength());
	}
	
	/** Saves the input from the console */
	private void saveInput() {
		if (!consoleInput.getText().isEmpty()) {
			lastCommand = consoleInput.getText();
			println(">> " + lastCommand);
			consoleInput.setText("");
		}
	}
	
	/** Returns the last command */
	public String getInput() {
		while (lastCommand == "") {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		consoleInput.requestFocus();
		String result = lastCommand;
		lastCommand = "";
		return result;
	}
	
	/** Get a normal string and ignore command shortcuts */
	public String getNormalInput() {
		normalStringRequested = true;
		lastCommand = "";
		while (lastCommand == "") {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		consoleInput.requestFocus();
		String result = lastCommand;
		lastCommand = "";
		normalStringRequested = false;
		return result;
	}
	
	/** The keylistener to register a submitted command */
	KeyListener inputListener = new KeyListener() {
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode()==KeyEvent.VK_ENTER){
		        saveInput();
		    } 
			if(!normalStringRequested) {
				// Game commands
				if(MyDungeon.game.running){
					if (e.getKeyCode()==KeyEvent.VK_UP){
				    	consoleInput.setText("up");
				        saveInput();
				    } else if (e.getKeyCode()==KeyEvent.VK_DOWN){
				    	consoleInput.setText("down");
				        saveInput();
				    } else if (e.getKeyCode()==KeyEvent.VK_LEFT){
				    	consoleInput.setText("left");
				        saveInput();
				    } else if (e.getKeyCode()==KeyEvent.VK_RIGHT){
				    	consoleInput.setText("right");
				        saveInput();
				    } else if (e.getKeyCode()==KeyEvent.VK_PERIOD){
				    	consoleInput.setText("wait");
				    	e.consume();
				        saveInput();
				    }
				}
				
				// System commands
			  	if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
			    	consoleInput.setText("exit");
			        saveInput();
			    } else if (e.getKeyCode()==KeyEvent.VK_F1){
			    	consoleInput.setText("help");
			        saveInput();
			    } else if (e.getKeyCode()==KeyEvent.VK_F5){
			    	consoleInput.setText("newgame");
			        saveInput();
			    }
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode()==KeyEvent.VK_PERIOD && MyDungeon.game.running){
				e.consume();
		        consoleInput.setText("");
		    }
		}
		@Override
		public void keyTyped(KeyEvent e) {}
	};
}